package com.test

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.async
import kotlinx.coroutines.newFixedThreadPoolContext
import org.junit.Test
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

class KotlinTest {


    private val lowCoroutineScope by lazy {
        CoroutineScope(newFixedThreadPoolContext(Runtime.getRuntime().availableProcessors(), "threadPool"))
    }


    @Test
    fun testKotlinThreadPool() {
        val time = "15/04/2021"
        println(SimpleDateFormat("dd/MM/yyyy", Locale.ROOT).parse(time).before(Date()))

        println(SimpleDateFormat("dd/MM/yyyy", Locale.ROOT).format(Date()))
//        println(array)
/*        val t = System.currentTimeMillis()
        val rootDir = File("E:\\download\\download")

        val allTxtFile = ArrayList<File>()
        prinltDIr(rootDir, allTxtFile)
        for (file in allTxtFile) {
            printTxt(file)
        }
        println("kotlin ${(System.currentTimeMillis() - t)},${allTxtFile.size}")
        println(getHorizontalTipArray(arrayOf("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10")).forEachIndexed { index, s ->
            if (index % 2 == 1) println(s)
            else print("$s ,")
        })*/
    }

    private fun getHorizontalTipArray(desArray: Array<String?>): Array<String?> {
        val middleIndex = (desArray.size and 1) + (desArray.size shr 1)
        val tipArray = arrayOfNulls<String>(desArray.size)
        for (tipIndex in (0 until middleIndex)) {
            val index = tipIndex shl 1
            tipArray[index] = desArray[tipIndex] ?: ""
            if (middleIndex + tipIndex < desArray.size)
                tipArray[index + 1] = desArray[middleIndex + tipIndex]
        }
        return tipArray
    }


    fun prinltDIr(rootDir: File, allTxtFile: ArrayList<File>) {
        val files = rootDir.listFiles()
        for (file in files) {
            if (file.isDirectory) {
                prinltDIr(file, allTxtFile)
            } else if (file.name.endsWith(".txt")) {
                allTxtFile.add(file)
            }
        }
    }

    private suspend fun printTxt(file: File?): String? {
        if (file == null || !file.exists()) return ""
        var result: String? = null
        val job = lowCoroutineScope.async {
            println("----------------------------------------------------" + file.absolutePath)
            result = file.readText()
            println(result)
        }
        job.await()
        return result;
    }


}



